package com.njym.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.njym.annotation.PermInfo;
import com.njym.entity.SysOrganization;
import com.njym.service.SysOrganizationService;
import com.njym.vo.Json;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
/**
 * Created with IDEA
 * author:张善兵
 * Date:2018/8/21
 * Time:下午1:39
 */
@PermInfo(value = "系统机构")
@RestController
@RequiresPermissions("a:sys:org")
@RequestMapping("/sys_org")
public class SysOrganizationController {
    private static final Logger log = LoggerFactory.getLogger(SysOrganizationController.class);
    @Autowired
    private SysOrganizationService sysOrganizationService;

    @PermInfo(value = "添加机构字典")
    @RequiresPermissions("a:sys:org:add")
    @PostMapping("/add")
    public Json addSysOrganization(@RequestBody String body){
        SysOrganization organization = JSON.parseObject(body, SysOrganization.class);
        String oper = "add sys organization";
        if (StringUtils.isEmpty(organization.getOrgName())) {
            return Json.fail(oper, "机构名不能为空");
        }
        if (StringUtils.isEmpty(organization.getCode())) {
            return Json.fail(oper, "机构编码不能为空");
        }
        SysOrganization organization1 = sysOrganizationService.selectOne(new EntityWrapper<SysOrganization>().eq("org_name", organization.getOrgName()));
        if (organization1 != null) {
            return Json.fail(oper, "机构已注册");
        }
        SysOrganization organization2 = sysOrganizationService.selectOne(new EntityWrapper<SysOrganization>().eq("code", organization.getCode()));
        if (organization2 != null) {
            return Json.fail(oper, "机构已注册");
        }
        organization.setDeleted(0);
        boolean success = sysOrganizationService.insert(organization);

        return Json.result(oper, success)
                .data("id",organization.getId())
                .data("orgName", organization.getOrgName());
    }


    @PermInfo(value = "物理删除机构")
    @RequiresPermissions("a:sys:org:logicDel")
    @PostMapping("/logicDel")
    public Json deleteSysOrganizationById(@RequestBody String body){

        String oper = "logicDel sys organization";
        SysOrganization organization = JSON.parseObject(body, SysOrganization.class);
        organization.setDeleted(1);

        boolean success = sysOrganizationService.updateById(organization);
        return Json.result(oper, success)
                .data("id", organization.getId())
                .data("orgName", organization.getOrgName());
    }

    @PermInfo(value = "修改系统机构")
    @RequiresPermissions("a:sys:org:update")
    @PostMapping("/update")
    public Json updateOrganization(@RequestBody String body){

        String oper = "update sys organization";
        SysOrganization organization = JSON.parseObject(body, SysOrganization.class);
        boolean success = sysOrganizationService.updateById(organization);

        return Json.result(oper, success)
                .data("id", organization.getId());
    }


    @PermInfo("查找系统机构")
    @RequiresPermissions("a:sys:org:query")
    @PostMapping("/query")
    public Json queryOrganization(@RequestBody String body) {
        String oper = "query org info";
        log.info("{}, body: {}", oper, body);

        JSONObject json = JSON.parseObject(body);
        int current = json.getIntValue("current");
        int size = json.getIntValue("size");
        if (current == 0) current = 1;
        if (size == 0) size = 10;
        Wrapper<SysOrganization> queryParams = new EntityWrapper<SysOrganization>();
        queryParams.orderBy("sort_no", true);
        queryParams.eq("deleted", 0);
        String orgName = json.getString("orgName");
        if (orgName != null && StringUtils.isNotEmpty(orgName)) {
            queryParams.like("org_name", orgName);
        }
        Page<SysOrganization> page = sysOrganizationService.selectPage(new Page<>(current, size), queryParams);
        return Json.succ(oper).data("page", page);
    }

}
